package com.cj.cms.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cj.cms.cache.ChannelArticleCache;
import com.cj.cms.dao.security.ArticleDao;
import com.cj.cms.dao.security.ChannelDao;
import com.cj.cms.entity.Article;
import com.cj.cms.entity.Channel;
import com.cj.common.orm.hibernate.EntityManager;
import com.cj.sys.dao.security.UserDao;
import com.cj.sys.entity.security.User;
import com.cj.sys.service.ServiceException;

@Service
@Transactional
public class ArticleManager extends EntityManager<Article, Long> {
	@Autowired
	private ArticleDao articleDao;
	@Autowired
	private UserDao userDao;
	@Autowired
	private ChannelDao channelDao;
	@Override
	public void save(Article entity) throws ServiceException {
		User admin = userDao.getCurrentUser();
		if(entity.getId() == null){
			entity.setCreateUser(admin);
		}
		entity.setModifyUser(admin);
		super.save(entity);
		if(entity.getChannel() != null && entity.getChannel().getId()!= null){
			Channel channel = channelDao.load(entity.getChannel().getId());
			entity.setFullName(channel.getFullName() + "/" + entity.getId());
		}
		articleDao.save(entity);
	}
	
	@Override
	protected void afterChange(Long id) {
		Article article = this.load(id);
		//清理缓存
		ChannelArticleCache.getInstance().removeArticlePage(article.getChannel().getCode());
	}
	@Override
	protected ArticleDao getEntityDao() {
		return articleDao;
	}
}
